package jp.ac.u_tokyo.dch.service;

import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.dao.common.PsiDao;
import jp.ac.u_tokyo.dch.dao.common.SeqenceGenerator;
import jp.ac.u_tokyo.dch.dao.data.DataDao;
import jp.ac.u_tokyo.dch.dao.data.DataDetailDao;
import jp.ac.u_tokyo.dch.dao.data.DataFileDao;
import jp.ac.u_tokyo.dch.model.db.Data;
import jp.ac.u_tokyo.dch.model.db.DataDetail;
import jp.ac.u_tokyo.dch.model.db.DataFile;
import jp.ac.u_tokyo.dch.model.db.DataSchemaDetail;
import jp.ac.u_tokyo.dch.util.common.DataInputUtil;
import jp.ac.u_tokyo.dch.view.outputdata.DataView;
/**
*
* @version 1.1
*
*/
public class DataService {
	/**
	* DBコネクションオブジェクト
	 */
	private DBConnection dbconn = null;

	/**
	 * オブジェクト生成者
	 * @param dbconn
	 */
	public DataService(DBConnection dbconn){
		this.dbconn = dbconn;
	}

	/**
	 * DBのデータを画面モデルに変換する
	 * @param dataId データID
	 * @param view 画面モデル
	 * @throws DBException 例外
	 */
	public void dbDataToView(String dataId, DataView view) throws DBException {

		DataDao dataDao = new DataDao(dbconn);
		DataDetailDao dataDetailDao = new DataDetailDao(dbconn);
		DataFileDao dataFileDao = new DataFileDao(dbconn);
		PsiDao psiDao = new PsiDao(dbconn);

		Data data = dataDao.selectById(dataId);
		List<DataDetail> list = dataDetailDao.selectById(dataId, "0");
		List<DataDetail> customList = dataDetailDao.selectById(dataId, "1");
		List<DataFile> dataFileList = dataFileDao.selectById(dataId);

		Map<String, DataSchemaDetail> detailMap = view.getDataSchemaDetailMap();

		for (DataDetail detail : list) {

			DataInputUtil.setDataDetailView(detail, detailMap, psiDao);
		}

		for (DataDetail customDetail : customList) {

			DataInputUtil.setDataDetailView(customDetail, detailMap, psiDao);
		}

		 view.setData(data);
		 view.setDataDetailList(list);
		 view.setCustomDataDetailList(customList);
		 view.setDataFileList(dataFileList);

	}


	/**
	 * 挿入する
	 * @param view 画面モデル
	 * @throws DBException 例外
	 */
	public void insert(DataView view) throws DBException {

		DataDao dataDao = new DataDao(dbconn);
		DataDetailDao dataDetailDao = new DataDetailDao(dbconn);
		DataFileDao dataFileDao = new DataFileDao(dbconn);

		SeqenceGenerator seqenceGenerator = new SeqenceGenerator(dbconn);

		int dataId = seqenceGenerator.getSequence(SeqenceGenerator.DATA_SEQ);

		Data data = view.getData();
		data.setId(String.valueOf(dataId));

		Map<String, DataSchemaDetail> detailMap = view.getDataSchemaDetailMap();

		List<DataDetail> dataDetailList = view.getDataDetailList();
		List<DataDetail> customDataDetailList = view.getCustomDataDetailList();
		List<DataFile> fileList = view.getDataFileList();

		DataInputUtil.viewDataToDbData(dbconn, dataDetailList, detailMap);
		DataInputUtil.viewDataToDbData(dbconn, customDataDetailList, detailMap);

		dataDao.insert(data);
		dataDetailDao.insertAll(dataDetailList, dataId);
		dataDetailDao.insertAll(customDataDetailList, dataId);
		dataFileDao.insertAll(fileList, dataId);

	}

	/**
	 * 更新する
	 * @param view 画面モデル
	 * @throws DBException 例外
	 */
	public void update(DataView view) throws DBException {

		DataDao dataDao = new DataDao(dbconn);
		DataDetailDao dataDetailDao = new DataDetailDao(dbconn);
		DataFileDao dataFileDao = new DataFileDao(dbconn);

		Data data = view.getData();

		int dataId = Integer.valueOf(data.getId());


		Map<String, DataSchemaDetail> detailMap = view.getDataSchemaDetailMap();

		List<DataDetail> dataDetailList = view.getDataDetailList();
		List<DataDetail> customDataDetailList = view.getCustomDataDetailList();
		List<DataFile> fileList = view.getDataFileList();

		DataInputUtil.viewDataToDbData(dbconn, dataDetailList, detailMap);
		DataInputUtil.viewDataToDbData(dbconn, customDataDetailList, detailMap);

		dataDao.update(data);
		dataDetailDao.delete(dataId);
		dataDetailDao.deleteItems(dataId);
		dataFileDao.delete(dataId);

		dataDetailDao.insertAll(dataDetailList, dataId);
		dataDetailDao.insertAll(customDataDetailList, dataId);
		dataFileDao.insertAll(fileList, dataId);

	}

	/**
	 * 削除する
	 * @param view 画面モデル
	 * @throws DBException 例外
	 */
	public void delete(DataView view) throws DBException {


		DataDao dataDao = new DataDao(dbconn);
		DataDetailDao dataDetailDao = new DataDetailDao(dbconn);
		DataFileDao dataFileDao = new DataFileDao(dbconn);

		Data data = view.getData();

		String id = data.getId();

		dataDao.delete(data);
		dataDetailDao.delete(Integer.valueOf(id));
		dataDetailDao.deleteItems(Integer.valueOf(id));
		dataFileDao.delete(Integer.valueOf(id));
	}

	/**
	 *
	 * @param name データ名
	 * @param id データID
	 * @return true:重複する名前がある、false:なし
	 * @throws DBException 例外
	 */
	public boolean checkName(String sort, String name, String id) throws DBException {

		DataDao dataDao = new DataDao(dbconn);

		return dataDao.checkName(sort, name, id);
	}


}
